#include <windows.h>
#include <commctrl.h>
#include "resource.h"

HINSTANCE ghInstance;

INT_PTR WINAPI SubDialogProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	switch (msg)
	{
		case WM_CLOSE:
			EndDialog(hWnd, 0);
			return TRUE;

		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDOK:
					return TRUE;

				case IDCANCEL:
					return TRUE;
			}
			break;

		default:
			return FALSE;
	}
	return FALSE;
}

INT_PTR WINAPI MainDialogProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	switch (msg)
	{
		case WM_CLOSE:
			EndDialog(hWnd, 0);
			return TRUE;

		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDOK:
					DialogBoxParam(ghInstance, MAKEINTRESOURCE(RES_Dialog_t2), 0, SubDialogProc, 0);
					{
						static int i;
						static char buf[32];
						sprintf(buf, "Sub Dialog Id: %d", i);
						MessageBox(0, buf, 0, 0);
						++i;
					}
					return TRUE;

				case IDCANCEL:
					DialogBoxParam(ghInstance, MAKEINTRESOURCE(RES_Dialog_t2), hWnd, SubDialogProc, 0);
					return TRUE;
			}
			break;

		default:
			return FALSE;
	}
	return FALSE;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	ghInstance = hInstance;
	InitCommonControls();
	DialogBoxParam(hInstance, MAKEINTRESOURCE(RES_Dialog_t1), 0, MainDialogProc, 0);
	MessageBox(0, "DialogBoxParam return", 0, 0);
	return 0;
}
